﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
//using StillDesign.PhysX;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using ModelImporter;
using System.IO;
using TestXNA3.Render;
using Microsoft.Xna.Framework.Graphics;

namespace TestXNA3.Resources
{
    public abstract class MeshResource : Resource
    {
        public Matrix World = Matrix.Identity;

        public bool CastShadows = false;

        public BoundingSphere BoundingSphere;

        private ShaderResource _shader = null;
        [System.ComponentModel.Category("MeshResource")]
        public ShaderResource Shader
        {
            get { return _shader; }
            set { _shader = value; }
        }

        [System.ComponentModel.Browsable(false)]
        public Vector3 Scale = Vector3.One;

        public MeshResource()
        {
        }

        public abstract void Render(ref Matrix view, ref Matrix proj, ref Vector3 ViewerLocation, SceneRenderer.EDrawMode drawMode);


        protected void DrawDefaultModel(Model Mesh)
        {
            if (Mesh == null)
                return;

            int passCount = Shader.GetPassCount();
            Shader.Begin();
            for (int i = 0; i < passCount; i++)
            {
                Shader.BeginPass(i);
                foreach (ModelMesh mesh in Mesh.Meshes)
                {
                    foreach (ModelMeshPart meshPart in mesh.MeshParts)
                    {
                        GameStartup.Graphics.VertexDeclaration = meshPart.VertexDeclaration;
                        GameStartup.Graphics.Vertices[0].SetSource(mesh.VertexBuffer,
                            meshPart.StreamOffset, meshPart.VertexStride);
                        GameStartup.Graphics.Indices = mesh.IndexBuffer;
                        GameStartup.Graphics.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                            meshPart.BaseVertex, 0, meshPart.NumVertices,
                            meshPart.StartIndex, meshPart.PrimitiveCount);
                    }
                }
                Shader.EndPass(i);
            }
            Shader.End();
        }
    }
}
